import server from './server.js';
import qs from 'qs';
import serive from './server.js';
function myServer(){
    this.server = serive;
}
myServer.prototype.v = function(ob){
    this.nowHandle = ob;
}
myServer.prototype.parseRouter = function(name,urlOb){
    var ob = this[name] = {};
    Object.keys(urlOb).forEach((item)=>{
        ob[item] = this.senMes.bind(this,name,item,urlOb[item]);
        ob[item].state = 'ready';
    })
}
myServer.prototype.senMes = function(moduleName,name,url,config){
    var config = config||{};

    var type = config.type||'post';
    var data = config.data||{};
    var self = this;
    //请求加遮罩

    //请求发送时
    var before = function(mes){   
        return mes;
    }
    //请求默认是自动绑定
    var defaultFn = function(mes){
        self.nowHandle[name] = mes.data;
        self[moduleName][name].state = 'ready';
    }
    // var success = config.success || defaultFn;
    var callback = function(res){
        defaultFn(res);
        typeof config.success == "function" && config.success(res);
    }
    var state = {
        get:function(){
            var urlqs = url + '?' + qs.stringify(data);
            server.get(urlqs).then(before).then(callback);
        },
        post:function(){
            server.post(url, data).then(before).then(callback);
        }
    };
    if(self[moduleName][name].state =='ready'){
        self[moduleName][name].state = 'pending';
        state[type]();
    }
}
export default new myServer;